package com.ymatou.iapi.userservice.testcase;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.SetNickNameDtoBean;
import com.ymatou.iapi.userservice.parameter.enums.ChannelSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.TerminalSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.UserTypeEnum;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.service.SetNickNameDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*设置昵称接口
*FreeMaker Template自动生成代码
*后台是根据Ymt_User_LoginIdUpdateHistory表里的更改记录来判断用户是否修改过昵称的
*/
@TestSuite
public class Ts_SetNickNameDto{
	private static SetNickNameDtoBean setnicknamedtoBean;
	private static SetNickNameDtoCall setnicknamedtoCall;
//	private static ULC201406Wapper userWapper;
	
	@BeforeClass
	public static void setUp() throws ClientProtocolException, IOException {
		Logger.createResultFile("设置昵称接口");
	}

	@Before
	public void caseUp() throws Exception {
		setnicknamedtoBean=new SetNickNameDtoBean();
		setnicknamedtoCall=new SetNickNameDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setnicknamedtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetNickNameDto_001() {
		Logger.start(true,"修改已存在的用户的昵称，成功");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = TestDataManager.getRandomNum(10)+"_nickName";
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();	
			Logger.verifyEquals(ECode.SUCCESS, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
//			Logger.verifyEquals("ok", setnicknamedtoCall.getString("Result"), "验证绑定成功");
			Userinfo info = new JUserWapper().selectUserinfoByUid(uid).get(0);
			Logger.verifyEquals(newNickName,  info.getLoginId(), "验证loginid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SetNickNameDto_002() {
		Logger.start(false,"修改不存在的用户的昵称，失败");
		try {
			String newNickName = TestDataManager.getRandomNum(7)+"_nickName";
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(Integer.MAX_VALUE);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();			

			Logger.verifyEquals("100001", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyIsContains("未查询到用户", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SetNickNameDto_003() {
		Logger.start(false,"输入已使用过的用户昵称，失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			
			CreateUserReq cur2=new CreateUserReq();
			cur2=JUserCallService.initCreateUserBean(cur2);
			Integer uid2=JUserCallService.createUserCall(cur2);
			
			setnicknamedtoBean.setNickName(cur2.getLoginId());
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();	
			
			Logger.verifyEquals("100007", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyIsContains("登录Id已被使用", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SetNickNameDto_004() {
		Logger.start(true,"多次修改昵称，只有第一次修改成功");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = TestDataManager.getRandomNum(7)+"_nickName";
			int usreId = uid;
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			Logger.comment("第一次修改昵称");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(usreId);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			//Logger.verifyEquals("ok", setnicknamedtoCall.getString("Result"), "验证绑定成功");
			
			Logger.comment("再尝试三次修改昵称");
			for(int i=0; i<3; i++){
				String nickName = TestDataManager.getRandomNum(7)+"_nickName";
				setnicknamedtoCall = new SetNickNameDtoCall();
				setnicknamedtoBean = new SetNickNameDtoBean();
				setnicknamedtoBean.setNickName(nickName);
				setnicknamedtoBean.setUserId(usreId);
				setnicknamedtoCall.setData(setnicknamedtoBean);
				setnicknamedtoCall.callService();
				Logger.verifyEquals("100030", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
				Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
				Logger.verifyIsContains("登录Id仅能被更改一次，您已更改过", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			}
			Userinfo info = new JUserWapper().selectUserinfoByUid(uid).get(0);
			Logger.verifyEquals(newNickName,  info.getLoginId(), "验证loginid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_005() {
		Logger.start(true,"未输入用户昵称，失败");
		try {
			setnicknamedtoBean.setUserId(100);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();			
			
			Logger.verifyEquals("100", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyIsContains("NickName 未提供", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_006() {
		Logger.start(false,"昵称为特殊字符，失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = "'\"&%* !@~.^\\_n";//21位
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();
			
			Logger.verifyEquals("100010", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyIsContains("登录Id内容错误", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_007() {
		Logger.start(false,"昵称以下划线开头，失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = "_nickName_";
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("100035", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyIsContains("登录Id首末位错误", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_008() {
		Logger.start(false,"昵称包含码头，失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = "码头"+TestDataManager.getRandomNum(7);
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("100037", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyIsContains("登录Id中存在特殊的不合法的字符", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_009() {
		Logger.start(false,"昵称包含'ymatou'，失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = TestDataManager.getRandomNum(7)+"ymatou";
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("100037", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyIsContains("登录Id中存在特殊的不合法的字符", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_010() {
		Logger.start(false,"昵称包含'yangmatou'，失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = TestDataManager.getRandomNum(7)+"yangmatou";
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("100037", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyIsContains("登录Id中存在特殊的不合法的字符", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_011() {
		Logger.start(false,"昵称全是数字，失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = TestDataManager.getRandomNum(7);
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("100036", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyIsContains("登录Id不能全是数字", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_012() {
		Logger.start(false,"昵称长度过长，失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = TestDataManager.getRandomNum(43);
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("100009", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyIsContains("登录Id长度超限（4-40）", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SetNickNameDto_013() {
		Logger.start(false,"修改别人的曾用名，失败");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			JUserCallService.SetNickName(uid, "NewName_" + TestDataManager.getRandomNum(10));
			
			CreateUserReq cur2=new CreateUserReq();
			cur2=JUserCallService.initCreateUserBean(cur2);
			Integer uid2=JUserCallService.createUserCall(cur2);
			
			setnicknamedtoBean.setNickName(cur.getLoginId());
			setnicknamedtoBean.setUserId(uid2);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();
			
			Logger.verifyEquals("100007", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyIsContains("登录Id已被使用", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetNickNameDto_014() {
		Logger.start(true,"修改回原来的昵称，成功");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = TestDataManager.getRandomNum(10)+"_nickName";
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(uid);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();	
			Logger.verifyEquals(ECode.SUCCESS, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
//			Logger.verifyEquals("ok", setnicknamedtoCall.getString("Result"), "验证绑定成功");
			Userinfo info = new JUserWapper().selectUserinfoByUid(uid).get(0);
			Logger.verifyEquals(newNickName,  info.getLoginId(), "验证loginid");
			
			setnicknamedtoBean.setNickName(cur.getLoginId());
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();	
			Logger.verifyEquals(ECode.SUCCESS, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
//			Logger.verifyEquals("ok", setnicknamedtoCall.getString("Result"), "验证绑定成功");
			info = new JUserWapper().selectUserinfoByUid(uid).get(0);
			Logger.verifyEquals(cur.getLoginId(),  info.getLoginId(), "验证loginid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SetNickNameDto_015() {
		Logger.start(true,"商家多次修改昵称，成功");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			cur.setUserType(UserTypeEnum.Seller);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = TestDataManager.getRandomNum(7)+"_nickName";
			int usreId = uid;
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			Logger.comment("第一次修改昵称");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(usreId);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			
			Logger.comment("再尝试三次修改昵称");
			for(int i=0; i<3; i++){
				String nickName = TestDataManager.getRandomNum(7)+"_nickName";
				setnicknamedtoCall = new SetNickNameDtoCall();
				setnicknamedtoBean = new SetNickNameDtoBean();
				setnicknamedtoBean.setNickName(nickName);
				setnicknamedtoBean.setUserId(usreId);
				setnicknamedtoCall.setData(setnicknamedtoBean);
				setnicknamedtoCall.callService();
				Logger.verifyEquals(ECode.SUCCESS, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
				Logger.verifyEquals("true", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
				newNickName=nickName;
			}
			Userinfo info = new JUserWapper().selectUserinfoByUid(uid).get(0);
			Logger.verifyEquals(newNickName,  info.getLoginId(), "验证loginid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SetNickNameDto_016() {
		Logger.start(true,"商家多次修改昵称超过100次，失败20170525之前产线修改过邮件确认");
		try {
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			cur.setUserType(UserTypeEnum.Seller);
			Integer uid=JUserCallService.createUserCall(cur);
			String newNickName = TestDataManager.getRandomNum(7)+"_nickName";
			int usreId = uid;
			int success=1;
			Logger.verifyNotEquals(cur.getLoginId(), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			Logger.comment("第一次修改昵称");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(usreId);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			
			Logger.comment("再尝试修改昵称");
			for(int i=0; i<101; i++){
				String nickName = TestDataManager.getRandomNum(12)+"_nickName";
				setnicknamedtoCall = new SetNickNameDtoCall();
				setnicknamedtoBean = new SetNickNameDtoBean();
				setnicknamedtoBean.setNickName(nickName);
				setnicknamedtoBean.setUserId(usreId);
				setnicknamedtoCall.setData(setnicknamedtoBean);
				setnicknamedtoCall.callService();
				if(setnicknamedtoCall.getString("Success").equals("true")){
					success++;
				}
			}
			
			Logger.verifyEquals("100030", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals(100,success, "验证商家允许修改昵称次数");
			Logger.verifyIsContains("登录Id仅能被更改一次，您已更改过", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}